package com.hkts.management.orderrecord.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.model.enums.OrderStatusEnum;
import com.hkts.management.orderrecord.entity.HktsOrderRecord;
import com.hkts.management.orderrecord.service.IHktsOrderRecordService;
import com.hkts.management.orderrecord.mapper.HktsOrderRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2021-04-22
 */
@Service
@Transactional
public class HktsOrderRecordServiceImpl extends ServiceImpl<HktsOrderRecordMapper, HktsOrderRecord> implements IHktsOrderRecordService {


    @Autowired
    private HktsOrderRecordMapper hktsOrderRecordMapper;

    @Override
    public IPage<HktsOrderRecord> selectPage(Page<HktsOrderRecord> page, HktsOrderRecord hktsOrderRecord) {
        QueryWrapper<HktsOrderRecord> query = Wrappers.query();
        QueryWrapper<HktsOrderRecord> entityWrapper = getEntityWrapper(query, hktsOrderRecord);
        return hktsOrderRecordMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsOrderRecord> selectList(HktsOrderRecord hktsOrderRecord) {
        QueryWrapper<HktsOrderRecord> query = Wrappers.query();
        query.lambda().orderByDesc(HktsOrderRecord::getCreateTime);
        QueryWrapper<HktsOrderRecord> entityWrapper = getEntityWrapper(query, hktsOrderRecord);
        return hktsOrderRecordMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsOrderRecord> getEntityWrapper(QueryWrapper<HktsOrderRecord> entityWrapper, HktsOrderRecord hktsOrderRecord) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsOrderRecord.getOrderId())) {
            entityWrapper.like(HktsOrderRecord.ORDER_ID, hktsOrderRecord.getOrderId());
        }
        if (StringUtils.isNotBlank(hktsOrderRecord.getRecord())) {
            entityWrapper.like(HktsOrderRecord.RECORD, hktsOrderRecord.getRecord());
        }
        /*if (StringUtils.isNotBlank(hktsOrderRecord.getCreateTime())) {
            entityWrapper.like(HktsOrderRecord.CREATE_TIME, hktsOrderRecord.getCreateTime());
        }
        if (StringUtils.isNotBlank(hktsOrderRecord.getUpdateTime())) {
            entityWrapper.like(HktsOrderRecord.UPDATE_TIME, hktsOrderRecord.getUpdateTime());
        }*/
        return entityWrapper;
    }
    @Override
    public void addOrderRecord(String orderId, String driverName,int orderSubStatus) {
        String record=driverName+ OrderStatusEnum.getResultEnum(orderSubStatus);
        HktsOrderRecord orderRecord=new HktsOrderRecord();
        orderRecord.setOrderId(orderId);
        orderRecord.setRecord(record);
        hktsOrderRecordMapper.insert(orderRecord);
    }
}
